import Vue from "vue";
import VeeValidate, { Validator } from "vee-validate";

const myValidate = {
  dictionary: {
    zh_CN: {
      messages: {
        required: field => "必填项，请输入",
        isDate: field => "日期必须符合yyyy-MM-dd格式",
        length: (field, args) => "输入长度不能超过 " + args[0] + "！",
        minlength: (field, args) => "输入长度不小于" + args[0] + "！",
        numPat: (field, args) =>
          "请输入数字,总位数不超过" +
          args[0] +
          ",小数位不超过" +
          args[1] +
          "位",
        iphone: field => "请输入正确的手机号码",
        email: field => "请输入正确的邮箱",
        postCode: field => "请输入正确的邮编",
        idNum: () => "请输入合法的身份证号码",
        decimal: (field, args) => "请输入正整数",
        percentage: () => "请输入0到100之间的数，小数位不超过2位",
        numMax: () => "数字过大",
        idNumTW: () => "请输入合法的台湾居民往来大陆通行证号码",
        idNumGA: () => "请输入合法的港澳居民往来内地通行证（回乡证）号码",
        idNumTWAndGA: () => "请输入合法的台湾/港澳居民通行证号码",
        min_value: (field, args) => "请输入≥" + args[0] + "的数！",
        max_value: (field, args) => "请输入≤" + args[0] + "的数！",
        space: field => "不能输入空格",
        china: field => "非中文字符请删除",
        notChina: field => "中文字符请删除",
        integerNum: field => "请输入大于0的数",
        uniSocialCode: field => "只允许输入18位由字母和数字组成", // 统一社会信用代码
        lettersAndNumbers: field => "只允许输入字母或数字",
        lettersOrNumbersOr_: field => "只允许输入字母、数字或者横杠"
      }
    }
  },
  isDate: {
    getMessage: field => "日期必须符合yyyy-MM-dd格式",
    validate(value) {
      let dateStr = "";
      // 可能传过来
      if (value.length < 10 || value[4] !== "-" || value[7] !== "-") {
        return false;
      }
      dateStr = new Date("" + value);
      if (isNaN(dateStr.getDate())) {
        return false;
      } else if (!/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/.test(value)) {
        return false;
      }
      const year = value.split("-")[0];
      const month = value.split("-")[1];
      const date = value.split("-")[2];
      const big_month = ["01", "03", "05", "07", "08", "10", "12"];
      const small_month = ["04", "06", "09", "11"];
      if (Number(year) < 1900) {
        // 要1900年以后
        return false;
      }
      // 被4整除但不能被100整除，或能被400整除的年份即为闰
      if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
        if (month === 2 && date > 29) {
          return false;
        }
      } else {
        if (month === 2 && date > 28) {
          return false;
        }
        let big_include = false;
        for (let i in big_month) {
          if (big_month[i] === month) {
            big_include = true;
            break;
          }
        }
        let small_include = false;
        for (let i in small_month) {
          if (small_month[i] === month) {
            small_include = true;
            break;
          }
        }
        if (month === 2 && date > 29) {
          return false;
        } else if (big_include && parseInt(date) > 31) {
          // big_month.includes(month)
          return false;
        } else if (small_include && parseInt(date) > 30) {
          // small_month.includes(month)
          return false;
        }
      }
      return true;
    }
  },
  length: {
    getMessage: (field, args) => "输入长度不能超过 " + args[0] + "！",
    validate(value, args) {
      if (typeof value === "number") {
        value = value.toString();
      }
      if (Number(args[0]) >= value.length) {
        return true;
      } else {
        return false;
      }
    }
  },
  minlength: {
    getMessage: (field, args) => "输入长度不小于" + args[0] + "！",
    validate(value, args) {
      if (typeof value === "number") {
        value = value.toString();
      }
      if (Number(args[0]) <= value.length) {
        return true;
      } else {
        return false;
      }
    }
  },
  numPat: {
    getMessage: (field, args) => {
      if (args[1] <= 0) {
        return "请输入正整数,总位数不超过" + args[0];
      }
      return (
        "请输入数字,总位数不超过" + args[0] + ",小数位不超过" + args[1] + "位"
      );
    },
    validate(value, args) {
      const max = parseInt(args[0]);
      const digit = parseInt(args[1]);
      const valueStr = "" + value;
      if (typeof value === "number") {
        value = value.toString();
      }
      if (!isNaN(value)) {
        if (valueStr.indexOf(".") > -1) {
          if (!value.split(".")[1]) {
            return false;
          } else if (
            valueStr.length <= max + 1 &&
            value.split(".")[1].length <= digit &&
            Number(valueStr) >= 0
          ) {
            return true;
          }
          return false;
        } else {
          if (valueStr.length <= max) {
            return true;
          }
          return false;
        }
      } else {
        return false;
      }
    }
  },
  iphone: {
    getMessage: field => "请输入正确的手机号码",
    validate(value) {
      if (/^1\d{10}$/.test(value)) {
        return true;
      } else {
        return false;
      }
    }
  },
  email: {
    getMessage: field => "请输入正确的邮箱",
    validate(value) {
      let re = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,6}$/;
      if (re.test(value)) {
        return true;
      } else {
        return false;
      }
    }
  },
  postCode: {
    getMessage: field => "请输入正确的邮编",
    validate(value) {
      let re = /^[1-9]\d{5}$/;
      if (re.test(value)) {
        return true;
      } else {
        return false;
      }
    }
  },
  idNum: {
    getMessage: field => "请输入合法的身份证号码",
    validate(value) {
      // 大陆身份证
      if (
        /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/.test(
          value
        )
      ) {
        return true;
      }
      return false;
    }
  },
  idNumTW: {
    getMessage: field => "请输入合法的台湾居民往来大陆通行证号码",
    validate(value) {
      // 台湾身份证
      if (/^[A-Z]\d{9}$/.test(value)) {
        return true;
      }
      return false;
    }
  },
  idNumGA: {
    getMessage: field => "请输入合法的港澳居民往来内地通行证（回乡证）号码",
    validate(value) {
      // 港澳身份证
      if (/^[A-Z]\d{7}$/.test(value)) {
        return true;
      }
      return false;
    }
  },
  idNumTWAndGA: {
    getMessage: field => "请输入合法的台湾/港澳居民通行证号码",
    validate(value) {
      // 台湾身份证/港澳身份证
      if (/^[A-Z]\d{9}$/.test(value) || /^[A-Z]\d{7}$/.test(value)) {
        return true;
      }
      return false;
    }
  },
  percentage: {
    getMessage: field => "请输入0到100之间的数，小数位不超过2位",
    validate(value) {
      if (/^(0|[1-9][0-9]?|100)(\.[0-9]?[0-9])?$/.test(value)) {
        if (Number(value) >= 0 && Number(value) <= 100) {
          return true;
        }
      }
      return false;
    }
  },
  numMax: {
    getMessage: field => "数字过大",
    validate(value) {
      if (!isNaN(value)) {
        if (Number(value) < Math.pow(2, 31)) {
          return true;
        }
      }
      return false;
    }
  },
  space: {
    getMessage: field => "不能输入空格",
    validate(value) {
      if (/^\S*$/.test(value)) {
        return true;
      }
      return false;
    }
  },
  china: {
    getMessage: field => "非中文字符请删除",
    validate(value) {
      if (/^[\u4E00-\u9FA5]+$/.test(value)) {
        return true;
      }
      return false;
    }
  },
  notChina: {
    getMessage: field => "中文字符请删除",
    validate(value) {
      if (!/^[\u4E00-\u9FA5]+$/.test(value)) {
        return true;
      }
      return false;
    }
  },
  integerNum: {
    getMessage: field => "请输入大于0的数",
    validate(value) {
      if (/[^\d.]/g.test(value)) {
        return false;
      }
      return true;
    }
  },
  uniSocialCode: {
    getMessage: field => "只允许输入18位由字母和数字组成",
    validate(value) {
      if (/^[a-z,A-Z,0-9]{18}$/g.test(value)) {
        return true;
      }
      return false;
    }
  },
  lettersAndNumbers: {
    getMessage: field => "只允许输入字母或数字",
    validate(value) {
      if (/^[a-z,A-Z,0-9]*$/g.test(value)) {
        return true;
      }
      return false;
    }
  },
  lettersOrNumbersOr_: {
    getMessage: field => "只允许输入字母、数字或者横杠",
    validate(value) {
      if (/^[-a-z,A-Z,0-9]*$/g.test(value)) {
        return true;
      }
      return false;
    }
  }
};

(function() {
  Vue.use(VeeValidate, { locale: "zh_CN", fieldsBagName: "fieldBags" });
  Validator.localize(myValidate.dictionary);
  Object.keys(myValidate).forEach(key => {
    if (key !== "dictionary") {
      Validator.extend(key, myValidate[key]);
    }
  });
})();
export default myValidate;
